package Servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ebook.entity.Ebook;
import cn.ebook.enume.ErrorEnum;
import cn.ebook.service.dao.impl.EbookServiceImpl;
import cn.ebook.util.DateFactory;
import cn.ebook.util.GsonUtil;
import cn.ebook.util.VoUtil;
import cn.ebook.vo.WebVo;

/**
 * 添加电子图书与控制
 */
public class AddServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8;");
		PrintWriter out = response.getWriter();
		String cid = request.getParameter("categoryid");
		String title = request.getParameter("title");
		String summary = request.getParameter("summary");
		String uploadMan = request.getParameter("uploadMan");
		String date = request.getParameter("date2");
		Ebook ebook = new Ebook();
		Long categoryId = new Long(cid);
		Date createDate = DateFactory.DateStr2Date(date);
		ebook.setCategoryId(categoryId);
		ebook.setTitle(title);
		ebook.setSummary(summary);
		ebook.setUploadUser(uploadMan);
		ebook.setCreateDate(createDate);
		EbookServiceImpl e = new EbookServiceImpl();
		Boolean bo = e.add(ebook);
		if (bo) {
			WebVo vo = VoUtil.getSuccess(null);
			System.out.println(vo.getMsg());
			String json = GsonUtil.getJson(vo);
			out.write(json);
			// String path=request.getContextPath();
			//
			// System.out.println("0"+path);
			// 测试下request对象获取各种路径
			// System.out.println("1"+request.getRemoteAddr());
			// System.out.println("2"+request.getServletPath());
			// System.out.println("4"+request.getSession().getServletContext().getContextPath());
			// System.out.println("5"+request.getRequestURL());
			// response.sendRedirect(path+"/EbookServlet");
			//
			// return;
		} else {
			// 添加失败
			WebVo vo = VoUtil.getErro(ErrorEnum.Ebook_Add_Fail);
			String json = GsonUtil.getJson(vo);
			out.write(json);

		}
		out.flush();
		out.close();
	}

}
/**
 * 
 * 
 * 根据测试结果得知 request.getContextPath()获取到项目路径————/项目名
 * 
 * 
 * 
 */
